package com.cnblogs.leizia.crm.workbench.web.controller;

import com.cnblogs.leizia.crm.commen.ErrorCode;
import com.cnblogs.leizia.crm.commen.RequestUri;
import com.cnblogs.leizia.crm.commen.Result;
import com.cnblogs.leizia.crm.settings.domain.User;
import com.cnblogs.leizia.crm.utils.DateTimeUtil;
import com.cnblogs.leizia.crm.utils.PrintJson;
import com.cnblogs.leizia.crm.utils.ServiceFactory;
import com.cnblogs.leizia.crm.utils.UUIDUtil;
import com.cnblogs.leizia.crm.vo.PaginationVo;
import com.cnblogs.leizia.crm.workbench.domain.ActivityRemark;
import com.cnblogs.leizia.crm.workbench.service.ActivityRemarkService;
import com.cnblogs.leizia.crm.workbench.service.impl.ActivityRemarkServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 市场活动备注控制器
 *
 * @author leizi
 * @create 2021-02-21 16:06
 */
public class ActivityRemarkController extends HttpServlet {

    private ActivityRemarkService activityRemarkService = (ActivityRemarkService) ServiceFactory.getService(new ActivityRemarkServiceImpl());

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        // 根据市场活动id获取市场活动备注列表
        if (RequestUri.ACTIVITY_REMARK_GET_LIST.getUriPath().equals(path)) {
            getRemarkList(req, resp);
        }
        // 分页获取备注记录
        if (RequestUri.ACTIVITY_REMARK_PAGE_LIST.getUriPath().equals(path)) {
            pageListRemark(req, resp);
        }
        // 根据id删除备注
        if (RequestUri.ACTIVITY_REMARK_DELETE.getUriPath().equals(path)) {
            deleteRemark(req, resp);
        }
        // 添加备注
        if (RequestUri.ACTIVITY_REMARK_ADD.getUriPath().equals(path)) {
            addRemark(req, resp);
        }
        // 修改备注
        if (RequestUri.ACTIVITY_REMARK_EDIT.getUriPath().equals(path)) {
            editRemark(req, resp);
        }
    }

    /**
     * 修改备注
     *
     * @param request
     * @param response
     * @return
     */
    private Result editRemark(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String remark = request.getParameter("remark");
        Map<String, String> remarkMap = new HashMap<>();
        remarkMap.put("id", id);
        remarkMap.put("noteContent", remark);
        User user = (User) request.getSession().getAttribute("user");
        // 修改人
        remarkMap.put("editBy", user.getName());
        // 修改时间
        remarkMap.put("editTime", DateTimeUtil.getSysTime());
        // 编辑状态
        remarkMap.put("editFlag", "1");

        boolean editRemark = activityRemarkService.editRemark(remarkMap);
        if (editRemark) {
            PrintJson.printJsonFlag(response, true);
        } else {
            PrintJson.printJsonObj(response, Result.errorResult(ErrorCode.ACTIVITY_EDIT_REMARK_ERROR.getMsg()));
        }
        return null;
    }

    /**
     * 添加备注信息
     *
     * @param request
     * @param response
     * @return
     */
    private Result addRemark(HttpServletRequest request, HttpServletResponse response) {
        String remark = request.getParameter("remark");
        String activityId = request.getParameter("activityId");
        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setActivityId(activityId);
        activityRemark.setNoteContent(remark);
        activityRemark.setId(UUIDUtil.getUUID());
        User user = (User) request.getSession().getAttribute("user");
        activityRemark.setCreateBy(user.getName());
        activityRemark.setCreateTime(DateTimeUtil.getSysTime());
        activityRemark.setEditFlag("0");

        boolean addRemark = activityRemarkService.addRemark(activityRemark);

        if (addRemark) {
            PrintJson.printJsonFlag(response, true);
        } else {
            PrintJson.printJsonObj(response, Result.errorResult(ErrorCode.ACTIVITY_ADD_REMARK_ERROR.getMsg()));
        }

        return null;
    }

    /**
     * 根据市场活动id分页获取备注记录
     *
     * @param request
     * @param response
     * @return
     */
    private Result pageListRemark(HttpServletRequest request, HttpServletResponse response) {
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        String activityId = request.getParameter("activityId");
        HashMap<String, Object> remarkMap = new HashMap<>();
        remarkMap.put("pageNo", pageNo);
        remarkMap.put("pageSize", pageSize);
        remarkMap.put("activityId", activityId);

        PaginationVo<ActivityRemark> paginationVo = activityRemarkService.pageListRemark(remarkMap);
        Result result = Result.successToDataResult(paginationVo);
        PrintJson.printJsonObj(response, result);

        return null;
    }

    /**
     * 根据id删除备注记录
     *
     * @param request
     * @param response
     * @return
     */
    private Result deleteRemark(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        // 删除备注记录
        boolean deleteRemark = activityRemarkService.deleteRemark(id);

        if (deleteRemark) {
            Result result = Result.successResult();
            PrintJson.printJsonObj(response, result);
        } else {
            PrintJson.printJsonObj(response, Result.errorResult(ErrorCode.ACTIVITY_DELETE_REMARK_ERROR.getMsg()));
        }

        return null;
    }

    /**
     * 根据市场活动id获取市场活动备注列表
     *
     * @param request
     * @param response
     * @return
     */
    private Result getRemarkList(HttpServletRequest request, HttpServletResponse response) {
        String activityId = request.getParameter("id");
        // 根据市场活动id获取备注列表
        List<ActivityRemark> remarkList = activityRemarkService.getRemarkList(activityId);

        Result result = Result.successToDataResult(remarkList);

        PrintJson.printJsonObj(response, result);

        return null;
    }
}
